# coding:utf-8
import web
import MySQLdb
import httplib,sys,os
import parserTpl
import PanelUtil 
urls=(
      '/(news\.do)', 'news',
      '/(top10panel\.do)', 'top10panel',
      '/(top10\.do)', 'top10',
      '/(stockpanel.do)','stockpanel',
      '/(stockpanelmini.do)','stockpanelmini',
      '/(stockpaneltiny.do)','stockpaneltiny',
      '/(stockpanellist.do)','stockpanellist',
      '/(.*)','index'
)

app=web.application(urls,globals())

#http://flashquote.stock.hexun.com/Stock_Combo.ASPX?mc=2_null&dt=Q,MI&t=0.7881548032255009
#http://flashquote.stock.hexun.com/
class index:
  def GET(self, name):
    par = web.input()
    parstr=""
    for i in par:
      parstr += i + "=" + par[i] + "&"
    parstr=parstr[:-1]
    conn = httplib.HTTPConnection("flashquote.stock.hexun.com")
    conn.request("GET", "/%s?%s" % (name, parstr))
    r1 = conn.getresponse()
    data = r1.read() 
    return data

class stockpanellist:
    def GET(self, name):
      conn=MySQLdb.connect(host="10.241.84.65",user="root",db="stock")
      conn.set_character_set('utf8')
      cursor = conn.cursor()
      tmp=web.input().topname
      tmp.encode("UTF-8")
      sql="select stock_class.code from stock_class,stock_info where stock_class.class='%s' and stock_info.code=stock_class.code order by stock_info.curwave desc" % (tmp)
      cursor.execute(sql)
      alldata=cursor.fetchall()
      res=""
      for i in alldata:
        flag="1"
        if i[0][0]!='6':
          flag="2"
        iframe = "<iframe frameborder='0' style='margin-bottom:-40px;width:950px;height:500px;margin-left:auto;margin-right:auto' src='/stockpanel.do?c=%s&m=%s'></iframe>" % (i[0], flag)
        res += iframe 
      dictData={"content":res} 
      return parserTpl.parserTpl(dictData, 'template/menu.tpl')
class stockpanel:
    def GET(self, name):
      pars=web.input()
      return parserTpl.expandStock(pars["c"].encode("UTF-8"))
class stockpanelmini:
    def GET(self, name):
      pars=web.input()
      return parserTpl.parserTpl({}, "template/stockpanelmini.tpl")
class stockpaneltiny:
    def GET(self, name):
      pars=web.input()
      return parserTpl.parserTpl({}, "template/stockpaneltiny.tpl")


def getTops():
  conn=MySQLdb.connect(host="10.241.84.65",user="root",db="stock")
  cursor = conn.cursor()
  sql = "select stock_class.des,stock_class.code,name,stock_info.curprice,stock_info.curwave from stock_class,stock_info where stock_class.code=stock_info.code;";
  cursor.execute(sql)
  alldata = cursor.fetchall()
  tops=dict()
  for i in alldata:
    print i[2]
    pstr=""
    if i[4] < 0:
      pstr = "%.2f%%" % (i[4])
    else:
      pstr = "+%.2f%%" % (i[4])
    wstr = "%.2f" % (i[3])
    if i[0] not in tops:
      tops[i[0]] = list()
    tops[i[0]].append([i[1],i[2],wstr,pstr])
  resstr="{"
  for i in tops:
    resstr += "'%s':[" % (i)
    print i
    for j in tops[i]:
      tmpstr="["
      for k in j:
        tmpstr+="'%s',"%(k)
      tmpstr = tmpstr[:-1] + "],"
      resstr += tmpstr;
    resstr = resstr[:-1] + "],"
  resstr = resstr[:-1] + "}"
  return resstr
class top10:
  def GET(self, name):
    return getTops()

#parserTpl.arserTpl({}, 'template/stockpanellist.tpl')
#top10.loadTopPanels()
class top10panel:
  def GET(self, name):
    res = PanelUtil.loadTopPanels() 
    dictData={"content":res}
    return parserTpl.parserTpl(dictData, 'template/menu.tpl')

class news:
  def GET(self, name):
    os.system("python parserBaiDuNews.py")
    conn=MySQLdb.connect(host="10.241.84.65",user="root",db="stock")
    #conn.set_character_set('utf8')
    cursor = conn.cursor()  
    sql = "select * from stock_news"
    cursor.execute(sql)
    alldata=cursor.fetchall()
    res=dict()
    for i in alldata:
      if i[0] not in res:
        res[i[0]] = list()
      res[i[0]].append(i)
    html = ""  
    html+="<div style='height:480px' width='1100px'><div class='topnewsbox'><ul class='newslist'>"
    for i in res["最新"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"
    html += "<div><div class='newsstocktab'><ul><li id='hushi' class='focus'>沪市</li><li id='shenshi'>深市</li><li id='ganggu'>全球股市</li></ul><table><tr><td>当前价：</td><td 'shprice'>100</td><td>涨幅：</td><td id='shwave'>1.2%</td></tr></table></div>"
    html += """<script type='text/javascript'>
function clearFocus(){
$('div.newsstocktab ul li').removeClass('focus');
$("div.newsstocktab ul li").css({"background-color":"#F0F0F0"})
}
$("li#hushi").click(function(){
  if($("li#hushi").hasClass('focus')){
    return;
  }
  clearFocus();
  $(this).addClass('focus');
  $("div.newsstocktab ul li.focus").css({"background-color":"#FFFFFF"})
  $("#stockflash").attr('src', '/stockpaneltiny.do?c=000001&m=1');
})
$("li#shenshi").click(function(){
  if($("li#shenshi").hasClass('focus')){
    return;
  }
  clearFocus();
  $(this).addClass('focus');
  $("div.newsstocktab ul li.focus").css({"background-color":"#FFFFFF"})
  $("#stockflash").attr('src', '/stockpaneltiny.do?c=399001&m=2');
})
$("li#quanqiu").click(function(){
  if($("li#quanqiu").hasclass('focus')){
    return;
  }
})
</script>"""  
    html += "<div>"  
    html += "<iframe id='stockflash' frameborder='0' style='margin-bottom:-40px;width:500px;height:370px;margin-left:30px;margin-right:auto' src='/stockpaneltiny.do?c=000001&m=1'></iframe>"
    html += "</div>"    
    html += "</div>"    
    html += "</div>"    

    html+="<div class='newsbox'><h2>大盘</h2><ul class='newslist'>"
    for i in res["大盘"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"

    html+="<div class='newsbox'><h2>个股</h2><ul class='newslist'>"
    for i in res["个股"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"
   
    html+="<div class='newsbox'><h2>热点板块</h2><ul class='newslist'>"
    for i in res["热点板块"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"

    html+="<div class='newsbox'><h2>新股</h2><ul class='newslist'>"
    for i in res["新股"]:
      html += "<li><a href='%s'>%s</a><span>%s</span></li>" % (i[1], i[2], i[3])
    html+="</ul></div>"
  
    dictData={"content":html}
    return parserTpl.parserTpl(dictData, 'template/menu.tpl')
if __name__=='__main__':
  app.run()
